<?php
/**
 * Created by PhpStorm.
 * User: 小夏
 * Date: 2016/10/8 0008
 * Time: 18:54
 */
namespace Invest\Controller;
use Common\Controller\HomebaseController;
class IndexController extends HomebaseController
{
    protected $course;
    protected $activity;
    protected $user;
    protected $holdings;
    protected $assetaccount;
    function _initialize() {
        parent::_initialize();
        $this->course = M("Course");
        $this->activity = M("Activity");
        $this->user = M("Users");
        $this->holdings = M("Holdings");
        $this->assetaccount = M("AssetAccount");
    }
    function index(){
        $course = $this->course->order('id desc')->limit('0,4')->select();
        if($course){
            $teacheerids = array();
            foreach ($course as $id){
                array_push($teacheerids,$id['uid']);
            }
            $teaname= $this->user->where(array('id'=>array('in',$teacheerids)))->getField('id,user_nicename,signature',true);
            foreach ($course as $key=>$value){
                $course[$key]['teaname'] = $teaname[$value['uid']]['user_nicename'];
                $course[$key]['signature'] = $teaname[$value['uid']]['signature'];
            }
        }
        $where = array();
        $classify = I('param.classify',2,'intval');
        if($classify){
            $where['act_classify']=$classify;
        }

        $activity = $this->activity->where($where)->order('id desc')->limit( '0,3')->select();

        $this->assign('course',$course);
        $this->assign('activity',$activity);
        $this->display(":index");
    }
    function api(){
        $user = $this->user->where(array('user_type'=>3))->select();
        $uid = array();
        foreach ($user as $key=>$value){
            array_push($uid,$value['id']);
        }

        $list = $this->holdings->where(array('uid'=>array('in',$uid)))->select();
        $uasset = $this->assetaccount->where(array('uid'=>array('in',$uid)))->getField('uid,initialmoney,usablemoney',true);

        foreach ($list as $key=>$value){
            $gupiao = get_stock_now_info($value['code']);
            $list[$key]['spotprice'] = $gupiao[3];
            $list[$key]['networth'] = $gupiao[3] * $value['number'];
            $list[$key]['closingtime'] = $gupiao[30];
            $list[$key]['initialmoney'] = $uasset[$value['uid']]['initialmoney'];
            $list[$key]['usablemoney'] = $uasset[$value['uid']]['usablemoney'];
        }

        $item=array();
        foreach($list as $k=>$v){
            if(!isset($item[$v['uid']])){
                $item[$v['uid']]=$v;
                unset($item[$v['uid']]['id']);
                unset($item[$v['uid']]['stockname']);
            }else{
                $item[$v['uid']]['networth']+=$v['networth'];
            }
        }
        print_r($item);exit;
    }
    function test(){
        $data['uid'] = '2';
        $data['networth'] = '2000000';
        $data['dayprice'] = '2.1';
        $data['weekprice'] = '2.2';
        $data['monthprice'] = '5.2';
        $data['earnings'] = '500000000';
        $data['property'] = '50000000';
        $data['meantime'] = '2.5';
        $User = M('StockLog');
        $User->data($data)->filter('strip_tags')->add();

    }
    function log($file,$txt)
    {
        $fp =  fopen($file,'ab+');
        fwrite($fp,'-----------'.time().'-----------------');
        fwrite($fp,$txt);
        fwrite($fp,"\r\n\r\n\r\n");
        fclose($fp);
    }
}